package com.wswy.wyjk.core

import android.arch.lifecycle.LiveData
import com.raft.framework.arch.SingleLiveData
import com.wswy.wyjk.db.DBHelper
import com.wswy.wyjk.model.LicenseType
import com.wswy.wyjk.model.LocalConfig
import com.wswy.wyjk.model.PracticeContext
import com.wswy.wyjk.model.toPracticeContext
import com.wswy.wyjk.utils.JsonKit
import com.wswy.wyjk.utils.Tools


object App {

    private const val KEY_CONFIG = "key_user_practice_type"

    private val practiceContextLiveData = SingleLiveData<PracticeContext?>()


    fun loadConfig(){
        practiceContextLiveData.postValue(hasType()?.toPracticeContext())
    }


    @JvmStatic
    var daoSession: DBHelper? = null


    private fun hasType():LocalConfig?{

        return kotlin.runCatching {
            JsonKit.GSON.fromJson(Tools.getCache().getString(KEY_CONFIG),LocalConfig::class.java)
        }.getOrNull()
    }

    @JvmStatic
    fun saveConfig(localConfig: LocalConfig){
        Tools.getCache().put(KEY_CONFIG,JsonKit.GSON.toJson(localConfig))

        practiceContextLiveData.postValue(localConfig.toPracticeContext())
    }

    @JvmStatic
    fun getPracticeContext():PracticeContext?{

        return practiceContextLiveData.value
    }

    fun getPracticeContextLiveData():LiveData<PracticeContext?> = practiceContextLiveData
}